import axios from "axios";
import qs from "qs";

const instance = axios.create({
  baseURL: "",
  timeout: 6000,
  headers: { "X-Custom-Header": "foobar" }
});
/*
  JWT模式下可能会用到
  const AUTH_TOKEN = "WSJABCDF-->TOKEN"
  instance.defaults.headers.common["Authorization"] = AUTH_TOKEN
*/
instance.interceptors.request.use(
  (config) => {
    //请求配置说明https://axios-http.com/zh/docs/req_config
    //比如这里可以加Authorization Beare Token
    // `withCredentials` 表示跨域请求时是否需要使用凭证
    // withCredentials: false, // default
    return config;
  },
  (err) => {
    return Promise.reject(err);
  }
);

instance.interceptors.response.use(
  (res) => {
    return res.data;
  },
  (err) => {
    return Promise.reject(err);
  }
);

export function $get(url, plainObj, cancelToken) {

  //get请求无需用qs.stringify(plainObj),直接plainObj即可
  return instance.get(url, { params: plainObj, cancelToken });
}
/*
 * axios POST提交数据的三种请求方式写法
 * https://segmentfault.com/a/1190000015261229
 */
export function $post(url, plainObj) {
  return instance.post(url, qs.stringify(plainObj));
  /*
  §§§
  用了qs.stringify之后,系统会自动写成'Content-Type': 'application/x-www-form-urlencoded',
  而且qs.stringify就应该与application/x-www-form-urlencoded搭配
  提交内容形如a=1237&b=encodeURIComponent('张三')
  经过qs.stringify转换过的数据,可$_POST直接取值.否则后台接收到的就是一个整体json字符串.
  §§§
  而JSON.stringify(plainObj)就该与application/json搭配使用
  /*
  /*
    ======另外一种写法=====
    const options = {
      url,
      data: qs.stringify(plainObj),
      method: 'POST',
      headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
      headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
    };
    return instance(options);
  */
}

export function $postFormData(url, formdata) {
  return instance.post(url, formdata, { headers: { "Content-Type": "multipart/form-data" } });
  /*
  会自动启用Content-Type: multipart/form-data,无需手写
  外层传入的plainObj需要形如↓
  let data = new FormData();
  data.append('code','1234');
  data.append('file',document.getElementById('f').files[0]);
  */
  /*
    ======另外一种写法=======
    const options = {
      url,
      method: 'POST',
      data: formdata, 
      headers: { 'Content-Type': 'multipart/form-data' }, 
    };
    return instance(options);
    */
}

export function $postJSON(url, plainObj) {
  return instance.post(url, plainObj, { headers: { "Content-Type": "application/json" } });
  //默认就用json形式发送出去Content-Type: application/json
  //提交形如{"aa":"圣斗士"},即相当于raw的json字符串
  //经过qs.stringify转换过的数据,可$_POST直接取值.否则后台接收到的就是一个整体json字符串.
  /*
    ======另外一种写法=======
    const options = {
      url,
      method: 'POST',
      data: plainObj,
      headers: { 'Content-Type': 'application/json' },
    };
    return instance(options);
    */
}

export function $del(url, plainObj) {
  return instance.delete(url, { data: qs.stringify(plainObj), headers: { "Content-Type": "application/x-www-form-urlencoded" } });
}
export function $put(url, plainObj) {
  return instance.put(url, { data: qs.stringify(plainObj), headers: { "Content-Type": "application/x-www-form-urlencoded" } });
}
